package com.cn.view;

import com.cn.bean.Department;
import com.cn.dao.DepartmentDao;
import com.cn.util.StringUtils;

import javax.swing.*;
import javax.swing.border.LineBorder;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/**
 * @Author Zhou Tao
 * @Date 2023/12/6 21:57
 */
public class DepartmentAddFrame extends JInternalFrame {
    private JTextField deptNameTextField;
    private JTextArea deptDescTextArea;

    public DepartmentAddFrame(){
        setClosable(true);
        setIconifiable(true);
        setTitle("添加部门界面");
        setBounds(100, 100, 450, 300);
        JLabel nameLabel = new JLabel("部门名称信息");
        JLabel descLabel = new JLabel("部门描述信息");
        deptNameTextField = new JTextField();
        deptNameTextField.setColumns(10);
        deptDescTextArea = new JTextArea();
        deptDescTextArea.setLineWrap(true);
        deptDescTextArea.setWrapStyleWord(true);

        // 点击"添加"按钮执行add()方法
        JButton addButton = new JButton("添加");
        addButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                // 获取部门名
                String deptName=deptNameTextField.getText();
                // 获取部门描述信息
                String deptDesc=deptDescTextArea.getText();
                // 添加部门
                add(deptName,deptDesc);
            }
        });
        addButton.setIcon(new ImageIcon(DepartmentAddFrame.class.getResource("/images/add.png")));

        // 点击"重置"按钮执行reset()方法
        JButton resetButton = new JButton("重置");
        resetButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                reset();
            }
        });
        resetButton.setIcon(new ImageIcon(DepartmentAddFrame.class.getResource("/images/reset.png")));
        GroupLayout groupLayout = new GroupLayout(getContentPane());
        GroupLayout.ParallelGroup parallelGroup1 = groupLayout.createParallelGroup(GroupLayout.Alignment.LEADING);
        GroupLayout.SequentialGroup sequentialGroup1 = groupLayout.createSequentialGroup();
        GroupLayout.ParallelGroup parallelGroup2 = groupLayout.createParallelGroup(GroupLayout.Alignment.LEADING);
        GroupLayout.SequentialGroup sequentialGroup2 = groupLayout.createSequentialGroup();
        sequentialGroup2.addComponent(nameLabel);
        sequentialGroup2.addPreferredGap(LayoutStyle.ComponentPlacement.UNRELATED);
        sequentialGroup2.addComponent(deptNameTextField,GroupLayout.PREFERRED_SIZE,185,GroupLayout.PREFERRED_SIZE);
        parallelGroup2.addGroup(sequentialGroup2);
        GroupLayout.SequentialGroup sequentialGroup3 = groupLayout.createSequentialGroup();
        GroupLayout.ParallelGroup parallelGroup3 = groupLayout.createParallelGroup(GroupLayout.Alignment.LEADING);
        parallelGroup3.addComponent(descLabel);
        parallelGroup3.addComponent(addButton);
        sequentialGroup3.addGroup(parallelGroup3);
        sequentialGroup3.addPreferredGap(LayoutStyle.ComponentPlacement.UNRELATED);
        GroupLayout.ParallelGroup parallelGroup4 = groupLayout.createParallelGroup(GroupLayout.Alignment.LEADING);
        parallelGroup4.addComponent(resetButton);
        parallelGroup4.addComponent(deptDescTextArea);
        sequentialGroup3.addGroup(parallelGroup4);
        parallelGroup2.addGroup(sequentialGroup3);
        sequentialGroup1.addGap(86);
        sequentialGroup1.addGroup(parallelGroup2);
        sequentialGroup1.addContainerGap(69, GroupLayout.PREFERRED_SIZE);
        parallelGroup1.addGroup(sequentialGroup1);
        groupLayout.setHorizontalGroup(parallelGroup1);
        GroupLayout.ParallelGroup pGroup1 = groupLayout.createParallelGroup(GroupLayout.Alignment.LEADING);
        GroupLayout.SequentialGroup group1 = groupLayout.createSequentialGroup();
        group1.addGap(56);
        GroupLayout.ParallelGroup pGroup2 = groupLayout.createParallelGroup(GroupLayout.Alignment.BASELINE);
        pGroup2.addComponent(nameLabel);
        pGroup2.addComponent(deptNameTextField, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE);
        group1.addGroup(pGroup2);
        group1.addGap(29);
        GroupLayout.ParallelGroup pGroup3 = groupLayout.createParallelGroup(GroupLayout.Alignment.LEADING);
        pGroup3.addComponent(descLabel);
        pGroup3.addComponent(deptDescTextArea, GroupLayout.PREFERRED_SIZE, 85, GroupLayout.PREFERRED_SIZE);
        group1.addGroup(pGroup3);
        group1.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED, 29, Short.MAX_VALUE);
        GroupLayout.ParallelGroup pGroup4 = groupLayout.createParallelGroup(GroupLayout.Alignment.BASELINE);
        pGroup4.addComponent(addButton);
        pGroup4.addComponent(resetButton);
        group1.addGroup(pGroup4);
        group1.addGap(28);
        pGroup1.addGroup(group1);
        groupLayout.setVerticalGroup(pGroup1);
        getContentPane().setLayout(groupLayout);
        deptDescTextArea.setBorder(new LineBorder(new java.awt.Color(127,157,185), 1, false));
    }

    /**
     * core code
     * 添加部门
     */
    private void add(String deptName,String deptDesc) {
        if(StringUtils.isEmpty(deptName)){
            JOptionPane.showMessageDialog(null, "部门名称不能为空！");
            return;
        }
        // 利用DepartmentDao添加部门
        DepartmentDao departmentDao=new DepartmentDao();
        Department department=new Department(deptName,deptDesc);
        int result=departmentDao.add(department);
        if(result==1){
            JOptionPane.showMessageDialog(null, "部门信息添加成功！");
            reset();
        }else{
            JOptionPane.showMessageDialog(null, "部门信息添加失败！");
        }
    }

    /**
     * core code
     * 重置
     */
    private void reset() {
        this.deptNameTextField.setText("");
        this.deptDescTextArea.setText("");
    }
}
